<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PersonController extends Controller
{

    //===================员工的添加=================
    public function getAdd(){
        $res = DB::table('department')
        ->select('id','DepartmentName')
        ->get();
        //dd($res);

        //查询所有部门
        $res1 =DB::table('department')
        ->join('post','post.DepartmentId','=','department.id')
        ->get();      
        // dd($res1);
       

        //解析添加模板页面
        return view('Personnel/Person/add',['res'=>$res,'res1'=> $res1]);
    }
    //执行添加操作
    public function postInsert(Request $request)
    {
        //查看所有数据
         // dd($request->all());

        //验证填写的数据
         $this->validate($request, [
        'PersonName' => 'required',
        'PersonID'=>'required|numeric|between:1000,9999',
        'UserName'=>'required|unique:person,UserName',
        'PassWord' => 'required',
        'Sex' =>'required',
        'Born' => 'required',
        'Education' => 'required',
        ],[
        'PersonName.required'=>'员工姓名不能为空',
        'PersonID.required'=>'工号必须不能为空',
        'PersonID.numeric'=>'工号必须为数字',
        'PersonID.between'=>'亲,工号必须在1000到5000之间呢',
        'UserName.required'=>'用户名不能为空',
        'UserName.unique'=>'用户名已存在',
        'PassWord.required'=>'密码不能为空',
        'Sex.required'=>'密码不能为空',
        'Born.required'=>'出生年月不能为空',
        'Education.required'=>'学历不能为空',
        ]);
        //dd($request->all());
         //以上验证成功之后,提取除了token之外的数据
         $data =$request ->only(['PersonName','PersonID','UserName','PassWord','Sex','Born','Education','PostID','DepartmentID']);
         //随机token
         // dd($data);
         $data['token'] = str_random(50);
         //加密 密码
         $data['PassWord'] = Hash::make($data['PassWord']);
         //执行数据添加
         $res =DB::table('person') ->insert($data);
         if($res){
            //成功跳转至人员列表页
            return redirect('Personnel/Person/index')->with('success','用户添加成功');
         }else{
            return back() ->with('error','用户添加失败');
         }

    }
    //文件上传
    public function upload(Request $request,$filename){
       
        //检测是否有文件上传
        if($request->hasFile($filename)){
            //随机文件名
            $name = md5(time()+rand(1,99999));
            //获取后缀名
            $su = $request->file($filename)->getClientOriginalExtension();
            $arr = array('jpg','png','jpeg');

            //if 
            if(!in_array($su,$arr)){
                echo ' <script type="text/javascript">
                    alert("上传文件不符合要求");
                    location.href="/Personnel/Person/add";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file($filename)->move('./uploads/',$name.'.'.$su);
            return '/uploads/'.$name.'.'.$su;
        }
    
 }


    //=========================员工显示=====================
    public function getIndex(Request $request){
        //查询分页
        //dd($request ->input('name'));

            if($request['name']){
                //搜索分页
                //echo 'aaa';
                $res = DB::table('person')
                ->join('department','department.id','=','person.DepartmentID')

                ->leftJoin('leaveoffice','leaveoffice.PersonID','=','person.id')

                ->join('post','post.PostId','=','person.PostID')
                ->where('person.PersonName','like','%'.$request['name'].'%')
                ->select('person.*', 'leaveoffice.Status', 'department.DepartmentName','post.PostName')
                //->get();
               ->orderby('person.id','asc')
                
                ->paginate(4);

                // dd($res);
            }else{
                //查询所有分页
                $res = DB::table('person') 
                ->join('department','department.id','=','person.DepartmentID')
               ->leftJoin('leaveoffice','leaveoffice.PersonID','=','person.id')
               ->join('post','post.PostId','=','person.PostID')   
               ->select('person.*', 'leaveoffice.Status', 'department.DepartmentName','post.PostName')
                // ->get();
               ->orderby('person.id','asc')

                ->paginate(3);
            }




           // dd($res);

        $info = $request -> all();
        //dd($person);
        //解析列表模板页面
        return view('Personnel/Person/index',['res'=>$res,'info'=>$info]);
    }





    //=================================员工编辑=====================
    public function getEdit($id){
       //dd($id);
        //根据id查询员工数据信息
        $info= DB::table('person')
        ->join('department','department.id','=','person.DepartmentID')
        ->leftJoin('leaveoffice','leaveoffice.PersonID','=','person.id')
        ->join('post','post.PostId','=','person.PostID')
        ->where('person.id',$id)
        ->select('person.*', 'leaveoffice.Status', 'department.DepartmentName','post.PostName')
        ->get();
        // dd($info);
        //解析模板显示编辑的页面
            return view('Personnel/Person.edit',['info'=>$info,'id'=>$id]);
    }
    //================执行编辑
    public function postUpdate(Request $request){
        $info = $request ->all();
        $id = $info['id'];
        DB::table('person')->where('id',$id)->update($info);
        return redirect('Personnel/Person/index')->with('修改成功');
    }   
    //=============================删除===========================
    public function getDel($id){
        //执行删除
        //dd($id);
        DB::table('person')->where('id',$id)->delete();
        //解析模板显示列表的页面
        return redirect('Personnel/Person/index')->with('删除成功');
    }

}
?>